import processing.core.*;
import controlP5.*;


//class MainGroup is basically a control group with a few more fields and methods
public class MainGroup  {
	
	String name;
	int xPos, yPos, width, height;
	ControlGroup theGroup;
	Canvas theCanvas;
	PApplet theApplet;
	
	
	public MainGroup(String _name, PApplet _theApplet, ControlP5 controlP5, int _xPos, int _yPos, int _width, int _height) {
		theApplet = _theApplet;
		name  = _name;
		xPos = _xPos;
		yPos = _yPos;
		width = _width;
		height = _height;	
		
		//creates a control group
		theGroup = controlP5.addGroup(name ,xPos, yPos, width);
		theGroup.setBackgroundHeight(height);
		theGroup.setBackgroundColor(theApplet.color(255,100));
		
		//adds a canvas to the control group
		//this may not be necessary in the future
		theCanvas = new Canvas(this);
		
	}
	
	//takes a mouse position and checks if it is in the groups bounds
	//then handles the event if this is the case by passing it to the canvas object
	public boolean handlePress( int mouseX, int mouseY) {
		if(this.theGroup.isOpen())
			if(mouseX > theGroup.absolutePosition().x && mouseX < (theGroup.absolutePosition().x + width))
				if(mouseY > theGroup.absolutePosition().y && mouseY < (theGroup.absolutePosition().y + height)) {
					theCanvas.event( mouseX, mouseY);
					return true;
				}
		return false;					
	}
	
	//takes a mouse position and checks if it is in the groups bounds when mouse button is released
	//if so, handles event by switching the active objects group
	public boolean handleRelease( DrawObject d, int mouseX, int mouseY ) {
		if(this.theGroup.isOpen())
			if(mouseX > theGroup.absolutePosition().x && mouseX < (theGroup.absolutePosition().x + width))
				if(mouseY > theGroup.absolutePosition().y && mouseY < (theGroup.absolutePosition().y + height)) {
					d.group.removeCanvas(d);
					d.switchGroup(this);
					theGroup.addCanvas(d);
					return true;
				}
		return false;	
	}

}
